package com.ocr.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.method.MovementMethod;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class GraphicView extends View implements OnTouchListener {
	public static int BOXTOPLEFT_TOUCHED = 1;
	public static int BOXTOPRIGHT_TOUCHED = 2;
	public static int BOXBOTTOMRIGHT_TOUCHED = 3;
	public static int BOXBOTTOMLEFT_TOUCHED = 4;

	public static int STEP_WIDTH = 7;
	public static int STEP_HEIGHT = 3;
	public static int DEFAULT_WIDTH = 70;
	public static int DEFAULT_HEIGHT = 30;

	public int width;
	public int height;

	public Box boxtopleft;
	public Box boxtopright;
	public Box boxbottomright;
	public Box boxbottomleft;

	public int boxwidth = DEFAULT_WIDTH;
	public int boxheight = DEFAULT_HEIGHT;

	private boolean switchColor = true;

	public boolean first = true;

	public GraphicView(Context context) {
		super(context);
		// initialBox();
		this.setOnTouchListener(this);
	}

	public GraphicView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// initialBox();
		this.setOnTouchListener(this);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		// width = this.getWidth();
		// height = this.getHeight();

		// int viewtop = this.getTop();
		// int viewright = this.getRight();
		// int viewbottom = this.getBottom();
		// int viewleft = this.getLeft();

		// int centerx = width / 2;
		// int centery = height / 2;

		if (first) {
			initialBox();
			first = false;
		}
		boxtopleft.draw(canvas);
		boxtopright.draw(canvas);
		boxbottomleft.draw(canvas);
		boxbottomright.draw(canvas);

		Paint paint = new Paint();
		// paint.setColor(Color.argb(75, 87, 217, 70));
		paint.setStrokeWidth(3);

		// view bound
		// canvas.drawLine(0, 0, width, 0, paint);
		// canvas.drawLine(width, 0, width, height, paint);
		// canvas.drawLine(width, height, 0, height, paint);
		// canvas.drawLine(0, height, 0, 0, paint);

		// box bound
		paint.setColor(Color.rgb(20, 241, 41));

		canvas.drawLine(boxtopleft.getRight(), boxtopleft.getBottom(),
				boxtopright.getLeft(), boxtopright.getBottom(), paint);
		canvas.drawLine(boxtopright.getLeft(), boxtopright.getBottom(),
				boxbottomright.getLeft(), boxbottomright.getTop(), paint);
		canvas.drawLine(boxbottomright.getLeft(), boxbottomright.getTop(),
				boxbottomleft.getRight(), boxbottomleft.getTop(), paint);
		canvas.drawLine(boxbottomleft.getRight(), boxbottomleft.getTop(),
				boxtopleft.getRight(), boxtopleft.getBottom(), paint);

	}

	public void initialBox() {

		width = this.getWidth();
		height = this.getHeight();

		Log.i("Size", width + " " + height);

		int centerx = width / 2;
		int centery = height / 2;

		boxtopleft = new Box(centerx - boxwidth - Box.DEFAULT_WIDTH, centery
				- boxheight - Box.DEFAULT_HEIGHT);
		boxtopright = new Box(centerx + boxwidth, centery - boxheight
				- Box.DEFAULT_HEIGHT);
		boxbottomright = new Box(centerx + boxwidth, centery + boxheight);
		boxbottomleft = new Box(centerx - boxwidth - Box.DEFAULT_WIDTH, centery
				+ boxheight);
	}

	// view center point
	public int getCenterX() {

		return getWidth() / 2;

	}

	// view center point
	public int getCenterY() {

		return getHeight() / 2;

	}

	public void increaseBoxSize() {

		if (boxwidth * 2 < getWidth() && boxheight * 2 < getHeight()) {
			increaseBoxHeight();
			increaseBoxWidth();
		}

	}

	public void increaseBoxWidth() {

		boxwidth += STEP_WIDTH;

	}

	public void increaseBoxHeight() {

		boxheight += STEP_HEIGHT;

	}

	public void decreaseBoxSize() {

		if (boxwidth > DEFAULT_WIDTH && boxheight > DEFAULT_HEIGHT) {
			decreaseBoxHeight();
			decreaseBoxWidth();
		}

	}

	public void decreaseBoxWidth() {

		boxwidth -= STEP_WIDTH;

	}

	public void decreaseBoxHeight() {

		boxheight -= STEP_HEIGHT;

	}

	public int runSwitchColor() {
		if (switchColor) {
			switchColor = false;
			return Color.rgb(241, 20, 41);
		} else {
			switchColor = true;
			return Color.rgb(20, 241, 41);
		}
	}

	public float getXOfTextBox() {
		return Math.min(boxtopleft.getRight(),boxtopright.getLeft());
	}

	public float getYOfTextBox() {
		return Math.min(boxtopleft.getBottom(),boxbottomleft.getTop());
	}

	public float getWidthOfTextBox() {
		return Math.abs(boxtopright.getLeft() - boxtopleft.getRight());
	}

	public float getHeightOfTextBox() {
		return Math.abs(boxbottomleft.getTop() - boxtopleft.getBottom());
	}

	private float startX;
	private float startY;
	private float endX;
	private float endY;

	private int touchedAt = 0;

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();
		if (action == MotionEvent.ACTION_DOWN) {
			touchActionDown(event);
		} else if (action == MotionEvent.ACTION_MOVE) {
			touchActionMove(event);
		} else if (action == MotionEvent.ACTION_UP) {
			touchActionUp(event);
		}

		return true;
	}

	public void touchActionDown(MotionEvent event) {
		startX =  event.getX();
		startY =  event.getY();

		touchedAt = touchAt2(startX, startY);
	}

	public void touchActionMove(MotionEvent event) {
		endX =  event.getX();
		endY =  event.getY();

		if (endX > 0 && endX < getWidth() && endY > 0 && endY < getHeight()) {
			float xstep = endX - startX;
			float ystep = endY - startY;
			startX = endX;
			startY = endY;
			move(xstep, ystep);
			this.invalidate();
		}
	}

	public void move(float xstep, float ystep) {
		if (touchedAt == BOXTOPLEFT_TOUCHED) {
			boxtopleft.moveTo(xstep, ystep);
			boxtopright.moveTo(0, ystep);
			boxbottomleft.moveTo(xstep, 0);
		} else if (touchedAt == BOXTOPRIGHT_TOUCHED) {
			boxtopright.moveTo(xstep, ystep);
			boxtopleft.moveTo(0, ystep);
			boxbottomright.moveTo(xstep, 0);
		} else if (touchedAt == BOXBOTTOMRIGHT_TOUCHED) {
			boxbottomright.moveTo(xstep, ystep);
			boxtopright.moveTo(xstep, 0);
			boxbottomleft.moveTo(0, ystep);
		} else if (touchedAt == BOXBOTTOMLEFT_TOUCHED) {
			boxbottomleft.moveTo(xstep, ystep);
			boxtopleft.moveTo(xstep, 0);
			boxbottomright.moveTo(0, ystep);
		}
	}

	public int touchAt2(float x, float y) {
		if (boxtopleft.isTouchMe(x, y))
			return BOXTOPLEFT_TOUCHED;
		else if (boxtopright.isTouchMe(x, y)) {
			return BOXTOPRIGHT_TOUCHED;
		} else if (boxbottomright.isTouchMe(x, y)) {
			return BOXBOTTOMRIGHT_TOUCHED;
		} else if (boxbottomleft.isTouchMe(x, y))
			return BOXBOTTOMLEFT_TOUCHED;

		return 0;
	}

	public int touchAT(float x, float y) {
		float boxtopleft_dis = boxtopleft.getDistance(x, y);
		float boxtopright_dis = boxtopright.getDistance(x, y);
		float boxbottomright_dis = boxbottomright.getDistance(x, y);
		float boxbottomleft_dis = boxbottomleft.getDistance(x, y);

		float min = Math.min(Math.min(boxtopleft_dis, boxtopright_dis),
				Math.min(boxbottomleft_dis, boxbottomright_dis));
		// Log.i("touched", boxtopleft_dis+" "+ boxtopright_dis+" "+bo)
		if (min == boxtopleft_dis)
			return BOXTOPLEFT_TOUCHED;
		else if (min == boxtopright_dis) {
			return BOXTOPRIGHT_TOUCHED;
		} else if (min == boxbottomright_dis) {
			return BOXBOTTOMRIGHT_TOUCHED;
		} else if (min == boxbottomleft_dis)
			return BOXBOTTOMLEFT_TOUCHED;

		return 0;
	}

	public void touchActionUp(MotionEvent event) {
		touchedAt = 0;
	}

}
